
from cnsenti import Sentiment
import investor

senti = Sentiment(pos="unformal_pos.txt", neg="unformal_neg.txt", merge=False, encoding="utf-8")

def load(path):

    lst = []
    with open(path,"r") as f:
        for line in f.readlines():
            line = line.strip()
            if line:
                guba = investor.Guba.fromString(line)
                lst.append(guba)
    return lst

def analysis_guba(guba):
    title = guba.title
    text = guba.news.news_text
    result = senti.sentiment_count(text)
    sen = investor.Senti()
    sen.words = result.get('words')
    sen.sentences = result.get('sentences')
    sen.pos = result.get('pos')
    sen.neg = result.get('neg')
    guba.senti = sen
    return result


def analysis(lst):
    for guba in lst:
        ret = analysis_guba(guba)
        print(ret)

    f = open("s_r.txt","w")
    f.write(investor.Guba.head()+'\n')
    for guba in lst:
        line = str(guba).replace('\xa0', '') + '\n'
        f.write(line)
        f.flush()
    f.close()


if __name__=="__main__":
    guba_lst = load("000625_长安汽车.txt")
    analysis(guba_lst)